package com.softeem.ossmusic.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.softeem.ossmusic.comm.R;
import com.softeem.ossmusic.entity.Album;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.softeem.ossmusic.service.AlbumService;
import com.softeem.ossmusic.service.MusicService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;

/**
 * 专辑管理：专辑列表、专辑详情、专辑添加、专辑修改、专辑删除
 */
@Tag(name = "专辑管理")
@RestController
public class AlbumController {

    @Autowired
    private AlbumService albumService;
    
    @Autowired
    private MusicService musicService;

    @Operation(summary = "专辑列表", description = "分页查询专辑列表")
    @Parameters({
            @Parameter(name = "page", description = "当前页码", required = true),
            @Parameter(name = "limit", description = "每页记录数", required = true)
    })
    @GetMapping("/album/list")
    public R list(@RequestParam(defaultValue = "1")Integer page, @RequestParam(defaultValue = "5")Integer limit) {
        //创建分页对象
        IPage<Album> ipage = new Page<Album>(page, limit);
        //执行查询(分页)
        albumService.page(ipage);
        //返回结果
        return R.success().setData(ipage.getRecords()).setCount(ipage.getTotal());
    }

    @Operation(summary = "所有专辑", description = "查询所有专辑列表")
    @GetMapping("/album/listAll")
    public R list(){
        return R.success().setData(albumService.list());
    }
    
    @Operation(summary = "专辑详情", description = "根据id查询专辑详情")
    @GetMapping("/album/details")
    public R details(Integer id){
        Album album = albumService.getById(id);
        if(album == null){
            return R.error().setMsg("专辑不存在");
        }
        return R.success().setData(album);
    }
    
    @Operation(summary = "添加专辑", description = "添加新专辑")
    @PostMapping("/album/add")
    public R add(@RequestBody Album album){
        if(albumService.save(album)){
            return R.success().setMsg("添加成功").setData(album);
        }
        return R.error().setMsg("添加失败");
    }
    
    @Operation(summary = "修改专辑", description = "根据ID修改专辑信息")
    @PutMapping("/album/update")
    public R update(@RequestBody Album album){
        if(album.getId() == null || albumService.getById(album.getId()) == null){
            return R.error().setMsg("专辑不存在");
        }
        if(albumService.updateById(album)){
            return R.success().setMsg("修改成功");
        }
        return R.error().setMsg("修改失败");
    }
    
    @Operation(summary = "删除专辑", description = "根据ID删除专辑")
    @DeleteMapping("/album/delete")
    public R delete(Integer id){
        if(id == null || albumService.getById(id) == null){
            return R.error().setMsg("专辑不存在");
        }
        if(albumService.removeById(id)){
            return R.success().setMsg("删除成功");
        }
        return R.error().setMsg("删除失败");
    }
    
    @Operation(summary = "获取专辑歌曲列表", description = "根据专辑ID查询专辑中的所有歌曲")
    @GetMapping("/album/getMusicList")
    public R getMusicList(Integer albumId){
        if(albumId == null || albumService.getById(albumId) == null){
            return R.error().setMsg("专辑不存在");
        }
        
        // 查询专辑中的所有歌曲
        QueryWrapper<com.softeem.ossmusic.entity.Music> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("aid", albumId);
        List<com.softeem.ossmusic.entity.Music> musicList = musicService.list(queryWrapper);
        
        return R.success().setData(musicList);
    }

}
